package com.gilight.base.controller;

import com.gilight.base.admin.order.AdminOrderService;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.dto.ColdPayOrderDto;
import com.gilight.base.dto.order.OrderDTO;
import com.gilight.base.dto.order.OrderStatisticsDTO;
import com.gilight.base.model.ColdOrder;
import com.gilight.base.model.Page;
import com.gilight.base.model.order.OrderDO;
import com.gilight.base.service.IColdOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/web/coldOrder")
public class ColdOrderController {

    @Autowired
    private IColdOrderService coldOrderServiceImpl;

    @Autowired
    private AdminOrderService adminOrderService;


    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "/createOrder", method = {RequestMethod.POST})
    public ResultEntity createOrder(@RequestBody @Validated ColdPayOrderDto coldPayOrderDto) {
        ResultEntity resultEntity = new ResultEntity();
        Long orderId = coldOrderServiceImpl.createOrder(coldPayOrderDto);
        resultEntity.setData(orderId);
        return resultEntity;
    }
    @ApiOperation(value = "查询订单列表")
    @RequestMapping(value = "/getOrderList", method = {RequestMethod.GET})
    public ResultEntity getOrderList(ColdOrder coldOrder,
                                     @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                                     @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        ResultEntity resultEntity = new ResultEntity();
        PageHelper.startPage(pageNo,pageSize);
        List<ColdPayOrderDto> list = coldOrderServiceImpl.getOrderList(coldOrder);
        PageInfo pageInfo = new PageInfo(list);
        resultEntity.setData(pageInfo);
        return resultEntity;
    }
    @ApiOperation(value = "查看订单详情")
    @RequestMapping(value = "/getOrderDetail", method = {RequestMethod.GET})
    public ResultEntity getOrderDetail(@RequestParam(value = "orderId")Long orderId) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(coldOrderServiceImpl.getOrderDetail(orderId));
        return resultEntity;
    }




    /**
     *
     * @param page    订单页码
     * @param limit   页码长度
     * @param status  订单状态
     * @param orderNo 订单号
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "查询订单列表")
    @RequestMapping(value = "/admin-list", method = {RequestMethod.GET})
    public ResultEntity<Page<OrderDO>> list(
            Integer page,
            Integer limit,
            Integer status,
            String orderNo,
            Long adminId,
            Long zzjgId,Long start,Long end) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.list(page,limit,status,orderNo,adminId,zzjgId,start,end));
        return resultEntity;
    }

    /**
     * 退款
     * @param orderNo
     * @param type        0.拒绝退款 1.同意退款
     * @param sum         退款金额
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "退款")
    @RequestMapping(value = "/admin-refund")
    public ResultEntity<String> refund(
            String orderNo,
            Integer type,
            Integer sum,
            Long adminId,
            Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.refund(orderNo,type,sum,adminId,zzjgId));
        return resultEntity;
    }

    /**
     * 发货
     * @param orderNo   订单号
     * @param shipCode  承运商Code
     * @param shipNo    运单号
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "发货")
    @RequestMapping(value = "/admin-ship")
    public ResultEntity<String> ship(
            String orderNo,
            String shipCode,
            String shipNo,
            Long adminId,
            Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.ship(orderNo,shipCode,shipNo,adminId,zzjgId));
        return resultEntity;
    }
    /**详情
     *
     * @param orderNo 订单Id
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "订单详情")
    @RequestMapping(value = "/admin-detail")
    public ResultEntity<OrderDTO> detail(
            Long orderNo,
            Long adminId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.detail(orderNo,adminId));
        return resultEntity;
    }

    /**
     * 根据传入时间和订单状态查询订单信息
     * @param gmtStart  查询开始时间
     * @param gmtEnd    查询结束时间
     * @param status    订单状态
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "根据传入时间和订单状态查询订单信息")
    @RequestMapping(value = "/admin-query-to-excel")
    public ResultEntity<List<OrderDTO>> queryToExcel(
            Long gmtStart,
            Long gmtEnd,
            Integer status,
            Long adminId,
            Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.queryToExcel(gmtStart,gmtEnd,status,adminId,zzjgId));
        return resultEntity;
    }

    /**
     * 编辑管理员备注
     * @param orderId  订单Id
     * @param level    备注等级
     * @param mono     备注
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "编辑管理员备注")
    @RequestMapping(value = "/admin-edit-admin-mono")
    public ResultEntity editAdminMono(
            Long orderId,
            Integer level,
            String mono,
            Long adminId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.editAdminMono(orderId,level,mono,adminId));
        return resultEntity;

    }

    /**
     * 订单统计
     * @param gmtStart 查询开始时间
     * @param gmtEnd   查询结束时间
     * @param adminId
     * @return
     * @throws ServiceException
     */
    @ApiOperation(value = "订单统计")
    @RequestMapping(value = "/admin-statistics")
    public ResultEntity<List<OrderStatisticsDTO>> statistics(
            Long gmtStart,
            Long gmtEnd,
            Long adminId,
            Long zzjgId) throws ServiceException{
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(adminOrderService.statistics(gmtStart,gmtEnd,adminId,zzjgId));
        return resultEntity;
    }

}
